#pragma once
#include <iostream>
#include<assert.h>
using namespace std;
namespace zkf
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		vector()
			:_s(nullptr)
			,_size(nullptr)
			,_capacity(nullptr)
		{

		}
		vector(size_t i, const T& j=T())
		{
			_s = new T[i];
			for (size_t q = 0; q < i; q++)
			{
				_s[q] = j;
			}
			_size = _s + i;
			_capacity = _size;
		}
		vector(int i, const T& j=T())
		{
			_s = new T[i];
			for (int q = 0; q < i; q++)
			{
				_s[q] = j;
			}
			_size = _s + i;
			_capacity = _size;
		}
		vector(const vector<T>& s)
		{
			for (auto e : s)
			{
				push_back(e);
			}
		}
		void swap(vector<T>& v)
		{
			std::swap(_s, v._s);
			std::swap(_size, v._size);
			std::swap(_capacity, v._capacity);
		}

		vector<T>& operator=(vector<T> v)
		{
			swap(v);

			return *this;
		}

		size_t size()const
		{
			return _size-_s;
		}
		size_t capacity()const
		{
			return _capacity - _s;
		}
		iterator begin()
		{
			return _s;
		}
		iterator end()
		{
			return _size;
		}
		const_iterator begin()const
		{
			return _s;
		}
		const_iterator end()const
		{
			return _size;
		}
		bool empty()const
		{
			return _size == _s;
		}
		void clear()
		{
			_size = _s;
		}
		void reserve(size_t i)
		{
			if (i > capacity())
			{
				size_t as = size();
				T* _tmp = new T[i];
				for (size_t i = 0; i < size(); i++)
				{
					_tmp[i] = _s[i];
				}
				delete[]_s;
				_s = _tmp;
				_size = _tmp + as;
				_capacity = _tmp + i;
			}
		}
		void push_back(const T& s)
		{
			if (_size == _capacity)
			{
				reserve(capacity()==0?4:2*capacity());
			}
			*_size = s;
			_size++;
		}
		void pop_back()
		{
			_size--;
		}
		void resize(size_t i,T s=T() )
		{
			if (_s + i <= _size)
			{
				_size = _s + i;
			}
			else
			{
				reserve(i);
				while (_size < _s + i)
				{
					*_size = s;
					_size++;
				}
			}
		}
		T& operator[](size_t i)
		{
			return _s[i];
		}
		const T& operator[](size_t i)const
		{
			return _s[i];
		}
		iterator insert(iterator s,const T& a)
		{
			if (_size == _capacity)
			{
				size_t len = s - _s;
				reserve(capacity() == 0 ? 4 : 2 * capacity());
				s = _s + len;
			}
			iterator q = _size;
			while (q>s)
			{
				*q = *(q - 1);
				q--;
			}
			*s = a;
			_size++;
			return s;
		}
		iterator erase(iterator s)
		{
			iterator a = s;
			while (a < _size-1)
			{
				*a = *(a + 1);
				a++;
			}
			_size--;
			return s;
		}
		template <class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last)
			{
				push_back(*first);
				++first;
			}
		}
		~vector()
		{
			delete[] _s;
			_s = nullptr;
			_size = nullptr;
			_capacity = nullptr;
		}
	private:
		iterator _s=nullptr;
		iterator _size=nullptr;
		iterator _capacity=nullptr;

	};
}